<?php
/**
 * User:于小涛
 * Date:2020/2/18
 * Time:13:40
 */
namespace app\admin\business\rbac;

use \app\admin\model\rbac\Role as RoleModel;
use app\admin\model\rbac\RoleAuth;

class Role {
    private $obj_role_model=null;
    private $obj_role_auth_model=null;
    public function __construct()
    {
        $this->obj_role_model = new RoleModel();
        $this->obj_role_auth_model = new RoleAuth();
    }
    /**
     * 获取所有组织架构树形结构
     * User:于小涛
     * Date:2020/2/18
     * Time:13:40
     * @param string $str_cpid 组织架构的pid
     * @param array $arr_role 查询结果
     * @param array $arr_exclude_role_id 需要排除的id
     * @param array $arr_checked_role_ids 当前管理员已经绑定的架构
     * @return array
     */
    public function getAllRoleTree($str_cpid='0',&$arr_role=array(),$arr_exclude_role_id=array(),$arr_checked_role_ids=array()){
        //首先获取顶级的全部由权限的菜单
        $arr_where=array(
            array('is_del','=',1),
            array('cpid','=',$str_cpid),
        );
        $arr_role=$this->obj_role_model->getList($arr_where,1,100000,'order_num desc');
        if(empty($arr_role)){
            $arr_role=array();
            return array();
        }
        foreach($arr_role as $int_id=>&$arr_item){
            //如果需要排除
            if(in_array($arr_item['id'],$arr_exclude_role_id)){
                unset($arr_role[$int_id]);
                continue;
            }
            if(in_array($arr_item['id'],$arr_checked_role_ids)){
                $arr_item['checked']=true;
            }
            $arr_item['child']=array();
            $this->getAllRoleTree($arr_item['cid'],$arr_item['child'],$arr_exclude_role_id,$arr_checked_role_ids);
            if(empty($arr_item['child'])){
                unset($arr_item['child']);
            }
        }
        return array();
    }
    /**
     * 获取权限select的option带html的字符串
     * User:于小涛
     * Date:2020/2/18
     * Time:13:40
     * @param string $str_cpid 组织架构的pid
     * @param string $str_selected_cid 当前的id，传递了则会自动选中对应的option
     * @param array $arr_exclude_role_id 需要排除的id
     * @return string $str_re;
     */
    public function getRoleHtmlOption($str_cpid='',$str_selected_cid=null,$arr_exclude_role_id=array()){
        $arr_role=array();
        $this->getAllRoleTree($str_cpid,$arr_role,$arr_exclude_role_id);
        $str_option_html='';
        $this->_getRoleHtmlOption($arr_role,$str_option_html,1,$str_selected_cid);
        return $str_option_html;
    }

    private function _getRoleHtmlOption($arr_role=array(),&$str_option_html='',$int_deep=1,$str_selected_cid=''){
        if(is_array($arr_role)){
            foreach($arr_role as $arr_item){
                if($int_deep==1){
                    $arr_item['name']="|--".$arr_item['name'];
                }else if ($int_deep>1){
                    $arr_item['name']="|--".str_pad("",($int_deep-1)*3,'-').$arr_item['name'];
                }
                $str_selected='';
                if($arr_item['cid']==$str_selected_cid){
                    $str_selected="selected";
                }
                $str_option_html.="<option value='{$arr_item['cid']}' {$str_selected}>{$arr_item['name']}</option>";
                if(!empty($arr_item['child'])){
                    $this->_getRoleHtmlOption($arr_item['child'],$str_option_html,$int_deep+1,$str_selected_cid);
                }
            }
        }
    }

    /**
     * 通过cpid计算当前的cid
     * User:于小涛
     * Date:2020/2/18
     * Time:14:35
     */
    public function getCidByCpid($str_pid){
        if(empty($str_pid)){
            $str_sql="select * from rbac_role WHERE LENGTH(cid)=3 ORDER BY cid DESC limit 1";
            $arr_data=$this->obj_role_model->query($str_sql);
            if(empty($arr_data)){
                $str_cid=100;
            }else{
                $str_cid=$arr_data[0]['cid']+1;
            }
        }else{
            $str_sql="select * from rbac_role WHERE cid LIKE '{$str_pid}___' ORDER BY cid DESC limit 1";
            $arr_data=$this->obj_role_model->query($str_sql);
            if(empty($arr_data)){
                $str_cid=$str_pid.'001';
            }else{
                $str_cid=$arr_data[0]['cid']+1;
            }
        }
        return $str_cid;
    }

    /**
     * 获取组织架构
     */
    public function getAuthListStruct(){
        $arr_list_role_mul=array();
        $arr_list_role_data=array();
        $this->getAllRoleTree('0',$arr_list_role_mul);
        $this->_proAuthToOne($arr_list_role_mul,$arr_list_role_data);
        return $arr_list_role_data;
    }
    /**
     * 将多维的权限处理为一维
     * @param array $arr_list_data 权限多维结构数组
     * @param array $arr_return_auth 最终返回的一级权限结构
     * @param int $int_deep 当前权限第几级，默认第一级
     * @return null
     */
    private function _proAuthToOne($arr_list_data=array(),&$arr_return_auth=array(),$int_deep=1){
        if(empty($arr_list_data)){
            return '';
        }
        foreach($arr_list_data as $arr_auth_item){
            $arr_tmp=$arr_auth_item;
            if($int_deep==1){
                $arr_tmp['name']="|--".$arr_tmp['name'];
            }else if ($int_deep>1){
                $arr_tmp['name']="|--".str_pad("",($int_deep-1)*3,'-').$arr_tmp['name'];
            }
            unset($arr_tmp['child']);
            $arr_return_auth[]=$arr_tmp;
            if(array_key_exists('child',$arr_auth_item)&&!empty($arr_auth_item['child'])){
                $this->_proAuthToOne($arr_auth_item['child'],$arr_return_auth,$int_deep+1);
            }
        }
    }

    /**
     * 给角色配置权限
     * User:于小涛
     * Date:2020/2/18
     * Time:16:07
     * @param $int_role_id
     * @param $arr_auth_ids
     * @return int
     * @throws \Exception
     */
    public function configAuth($int_role_id,$arr_auth_ids){
        $this->obj_role_auth_model->delByRoleId($int_role_id);
        $arr_add_data=array();
        foreach($arr_auth_ids as $int_auth_id){
            $arr_add_data[]=array(
                'role_id'=>$int_role_id,
                'auth_id'=>$int_auth_id
            );
        }
        return $this->obj_role_auth_model->add($arr_add_data);
    }

    /**
     * 获取角色树
     * User:于小涛
     * Date:2020/2/18
     * Time:16:25
     * @param $int_admin_id 管理员id，传递了做选中效果
     * @return array
     */
    public function getRoleTree($int_admin_id=null){
        $arr_current_role_ids=array();
        if($int_admin_id) {
            $str_sql = "select * from rbac_admin_role where admin_id=" . $int_admin_id;
            $arr_data = $this->obj_role_auth_model->query($str_sql);
            foreach ($arr_data as $arr_item) {
                $arr_current_role_ids[] = $arr_item['role_id'];
            }
        }
        $arr_list_data=array();
        $this->getAllRoleTree('0',$arr_list_data,array(),$arr_current_role_ids);
        recursionReplaceArrayKey($arr_list_data,array('id'=>'value','child'=>'list'));
        return $arr_list_data;
    }
}